import Vue from 'vue'
import Layout from '@/layout/index'
import Router from 'vue-router'
Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

export const constantRoutes = [{
  path: '/',
  redirect: '/login'
}, // 重定向,指向了home组件

{
  path: '/homePage',
  component: Layout,
  redirect: '/homePage/index',
  name: 'homePage',
  userType: 1,
  meta: { title: '商城首页', icon: 'tree' },
  children: [
    {
      path: 'index',
      name: 'index',
      component: () =>
      import ('@/views/homePage/index'),
      meta: { title: '首页', icon: 'list', keepAlive: false }
    },
    {
      path: 'productList',
      name: 'productList',
      component: () =>
      import ('@/views/productList/index'),
      meta: { title: '商品列表', icon: 'list', keepAlive: false }
    },
    {
      path: 'productDetail',
      name: 'productDetail',
      component: () =>
      import ('@/views/productDetail/index'),
      meta: { title: '商品详情', icon: 'list', keepAlive: false }
    }
  ]
},
{
  path: '/orderPage',
  component: Layout,
  redirect: '/orderList/index',
  name: 'orderList',
  userType: 1,
  meta: { title: '订单', icon: 'tree' },
  children: [
    {
      path: 'orderList',
      name: 'orderList',
      component: () =>
      import ('@/views/orderPage/orderList/index'),
      meta: { title: '订单列表', icon: 'list', keepAlive: false }
    },
    {
      path: 'orderDetail',
      name: 'orderDetail',
      component: () =>
      import ('@/views/orderPage/orderDetail/index'),
      meta: { title: '订单详情', icon: 'list', keepAlive: false }
    }
  ]
},
{
  path: '/cart',
  component: Layout,
  redirect: '/cartList/index',
  name: 'cartList',
  userType: 1,
  meta: { title: '购物车', icon: 'tree' },
  children: [
    {
      path: 'cartList',
      name: 'cartList',
      component: () =>
      import ('@/views/cart/cartList/index'),
      meta: { title: '购物车', icon: 'list', keepAlive: false }
    }
  ]
},
{
  path: '/submitOrder',
  component: Layout,
  redirect: '/getOrderInfo/index',
  name: 'getOrderInfo',
  userType: 1,
  meta: { title: '订单结算', icon: 'tree' },
  children: [
    {
      path: 'getOrderInfo',
      name: 'getOrderInfo',
      component: () =>
      import ('@/views/submitOrder/getOrderInfo/index'),
      meta: { title: '订单结算', icon: 'list', keepAlive: false }
    },
    {
      path: 'orderPay',
      name: 'orderPay',
      component: () =>
      import ('@/views/submitOrder/orderPay/index'),
      meta: { title: '支付页', icon: 'list', keepAlive: false }
    },
    {
      path: 'paySuccess',
      name: 'paySuccess',
      component: () =>
      import ('@/views/submitOrder/paySuccess/index'),
      meta: { title: '支付成功页', icon: 'list', keepAlive: false }
    }
  ]
},
{
  path: '/login',
  name: '登录',
  component: () =>
            import ('@/views/login/index'),
  hidden: true
},
{
  path: '/findPwd',
  name: '忘记密码',
  component: () =>
            import ('@/views/findPwd/index')
},
{
  path: '/404',
  component: () =>
            import ('@/views/error-page/404'),
  hidden: true
}

]
const asyncRoutes = [
  { path: '*', redirect: '/404', hidden: true }
]
const createRouter = () => {
  return new Router({
    mode: 'history',
    routes: constantRoutes,
    scrollBehavior: () => ({ y: 0 }),
    base: process.env.VUE_APP_BASE_ROUTER_NAME
  })
}

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}
export default router
export { asyncRoutes }

